<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜单管理</title>
    <link rel="stylesheet" href="{__CSS__}/element_ui.css"/>
    <link rel="stylesheet" href="{__CSS__}/style.css"/>
    <script src="{__JS__}/vue2.js"></script>
    <script src="{__JS__}/element_ui.js"></script>
    <script src="{__JS__}/axios.min.js"></script>
    <script src="{__JS__}/request.js"></script>
</head>
<body>
<div id="app">
    <div class="app-loading" v-if="pageLoading">
        <div class="app-loading__logo">
            <img src="{__IMG__}/logo.png"/>
        </div>
        <div class="app-loading__loader"></div>
        <div class="app-loading__title">{$title}</div>
    </div>
    <el-card class="box-card" v-else>
        <div slot="header" class="clearfix">
            <span>菜单管理</span>
        </div>
        <el-button type="primary" size="small" @click="addTop"><i class="el-icon-plus"></i> 添加顶级菜单</el-button>
        <div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-top: 10px"></div>
        <el-table
                :data="tableData"
                style="width: 100%;"
                row-key="id"
                :tree-props="{children: 'child'}">
            <el-table-column
                    type=""
                    prop="id"
                    width="120px"
                    label="编号">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="菜单名">
            </el-table-column>
            <el-table-column
                    prop="path"
                    label="路由">
            </el-table-column>
            <el-table-column
                    prop="type"
                    label="标识">
            </el-table-column>
            <el-table-column
                    label="图标">
                <template slot-scope="scope">
                    <i :class="scope.row.icon"></i>
                </template>
            </el-table-column>
            <el-table-column
                    label="类型">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.is_menu == 1">功能</el-tag>
                    <el-tag type="success" v-else>菜单</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="sort"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="operation"
                    label="操作">
                <template slot-scope="scope">
                    <el-button @click="add(scope.row)" type="text" size="small" v-if="scope.row.is_menu == 2">添加子菜单</el-button>
                    <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
                    <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                :close-on-click-modal="false"
                width="40%">
           {include file="/menu/save"}
        </el-dialog>
    </el-card>
</div>
<script>

    var app = new Vue({
        el: '#app',
        data: {
            pageLoading: true,
            tableData: [],
            dialogVisible: false,
            title: '添加菜单',
            baseIndex: '/{:config("shop.backend_index")}/',
            mode: 'add',
            form: {
                id: 0,
                pid: 0,
                type: 'system',
                name: "",
                path: "",
                icon: '',
                is_menu: '1',
                sort: 0
            },
            pName: "",
            rules: {
                name: [
                    {required: true, message: '请输入菜单名', trigger: 'blur'}
                ],
                type: [
                    {required: true, message: '请输入菜单标识', trigger: 'blur'}
                ],
                path: [
                    {required: true, message: '请输入权限路由', trigger: 'blur'}
                ],
            },
        },
        mounted() {
            this.getList()
            this.pageLoading = false
        },
        methods: {
            // 初始化表单
            initForm() {
                this.form = {
                    id: 0,
                    pid: 0,
                    type: 'system',
                    name: "",
                    path: "",
                    icon: '',
                    is_menu: '1',
                    sort: 0
                }
            },
            // 获取菜单列表
            async getList() {
                let res = await request.get(this.baseIndex + 'menu/index')
                this.tableData = res.data
            },
            add(row) {
                this.title = '添加子菜单';
                this.initForm()
                this.form.pid = row.id;
                this.pName = row.name;
                this.mode = 'add';

                this.dialogVisible = true;
            },
            edit(row) {
                this.form.id = row.id
                this.form.pid = row.pid
                this.form.type = row.type
                this.form.name = row.name
                this.form.path = row.path
                this.form.icon = row.icon
                this.form.is_menu = String(row.is_menu)
                this.form.sort = row.sort

                this.mode = 'edit';

                this.dialogVisible = true;
            },
            del(row) {
                this.$confirm('确定删除此菜单?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    let res = await request.get(this.baseIndex + 'menu/del', {id: row.id})
                    if (res.code == 0) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        setTimeout(() => {
                            this.getList();
                        }, 500)
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.msg
                        });
                    }
                }).catch(() => {});
            },
            addTop() {
                this.title = '添加顶级菜单';
                this.initForm()

                this.form.pid = 0;
                this.pName = '顶级菜单';
                this.mode = 'add';
                this.dialogVisible = true;
            },
            submitForm(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        let url = this.baseIndex + 'menu/add';
                        if (this.mode == 'edit') {
                            url = this.baseIndex + 'menu/edit';
                        }

                        let res = await request.post(url, this.form)
                        if (res.code == 0) {
                            this.$message.success(res.msg);
                            this.dialogVisible = false
                            setTimeout(() => {
                                this.getList();
                            }, 500)
                        } else {
                            this.$message.error(res.msg);
                        }
                    }
                });
            }
        },
    })
</script>
<style>
    a {text-decoration:none;}
</style>
</body>
</html>